package me.firstapp.spi.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.system.JsonOperator;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.system.OperatorService;

@Service
public class OperatorServiceImpl extends BaseServiceImpl implements OperatorService {

	@Override
	public SingleObject<JsonOperator> findOperatorById(Long id) {
		SingleObject<JsonOperator> result = new SingleObject<JsonOperator>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			JSONObject resultObject = api(parameters, "/operator/findOperatorById.do", HttpMethod.GET, "通过id查询操作员");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonOperator operator = JSONObject.toJavaObject(resultObject.getJSONObject("object"),
						JsonOperator.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(operator);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonOperator> addOperator(String email, String password, String name) {
		SingleObject<JsonOperator> result = new SingleObject<JsonOperator>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("email", email);
			parameters.put("password", password);
			parameters.put("name", name);
			JSONObject resultObject = api(parameters, "/operator/addOperator.do", HttpMethod.POST, "新增操作员");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonOperator operator = JSONObject.toJavaObject(resultObject.getJSONObject("object"),
						JsonOperator.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(operator);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonOperator> login(String name, String password, String code) {
		SingleObject<JsonOperator> result = new SingleObject<JsonOperator>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("name", name);
			parameters.put("password", password);
			parameters.put("code", code);
			JSONObject resultObject = api(parameters, "/operator/login.do", HttpMethod.POST, "操作员登录");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonOperator operator = JSONObject.toJavaObject(resultObject.getJSONObject("object"),
						JsonOperator.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(operator);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonOperator> findOperatorPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		PageObject<JsonOperator> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/operator/findOperatorPage.do", "分页查询操作员", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonOperator> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonOperator> operatorList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonOperator operator = JSONObject.toJavaObject(_jsonObject, JsonOperator.class);
								operatorList.add(operator);
							}
						}
					}
					jsonPage.setList(operatorList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonOperator> updateStatus(Long id, Integer status) {
		SingleObject<JsonOperator> result = new SingleObject<JsonOperator>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("status", status);
			JSONObject resultObject = api(parameters, "/operator/updateStatus.do", HttpMethod.POST, "修改操作员状态");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonOperator operator = JSONObject.toJavaObject(resultObject.getJSONObject("object"),
						JsonOperator.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(operator);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonOperator> alterPassword(Long id, String oldPassword, String newPassword, String code) {
		SingleObject<JsonOperator> result = new SingleObject<JsonOperator>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("oldPassword", oldPassword);
			parameters.put("newPassword", newPassword);
			parameters.put("code", code);
			JSONObject resultObject = api(parameters, "/operator/alterPassword.do", HttpMethod.POST, "修改操作员密码");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonOperator operator = JSONObject.toJavaObject(resultObject.getJSONObject("object"),
						JsonOperator.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(operator);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonOperator> findOperatorByName(String name) {
		SingleObject<JsonOperator> result = new SingleObject<JsonOperator>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("name", name);
			JSONObject resultObject = api(parameters, "/operator/findOperatorByName.do", HttpMethod.GET, "通过id查询操作员");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JsonOperator operator = JSONObject.toJavaObject(resultObject.getJSONObject("object"),
						JsonOperator.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(operator);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
